package com.sxjh.controller.ny;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxjh.annotation.UserLog;
import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.common.ResultException;
import com.sxjh.common.ZPage;
import com.sxjh.config.Authorization;
import com.sxjh.entity.Material;
import com.sxjh.entity.PlannedOrdersEntity;
import com.sxjh.entity.Task;
import com.sxjh.entity.param.MaterialAddParam;
import com.sxjh.entity.param.MaterialListParam;
import com.sxjh.entity.result.MaterialListResult;
import com.sxjh.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 物料  前端控制器
 * </p>
 *
 * @author zixin
 * @since 2021-06-05
 */
@RestController
@RequestMapping("/material")
@Api(tags = "物料")
public class MaterialController extends BaseController {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private MateriaLoadService materiaLoadService;

    @Autowired
    private MateriaUnloadService materiaUnloadService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private PlannedOrdersService plannedOrdersService;
    @Autowired
    private GoodsLocationService goodsLocationService;

    @ApiOperation("列表")
    @GetMapping
    @Authorization("material.list")
    Result<ZPage<MaterialListResult>> list(@Validated MaterialListParam param) {
        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        // 根据物料名称搜索
        if (StrUtil.isNotEmpty(param.getName())) wrapper.like(Material::getName, param.getName());
        IPage iPage = getIPage(param);
        wrapper.orderByDesc(Material::getId);
        iPage = materialService.page(iPage, wrapper);
        ZPage<MaterialListResult> zPage = copyPage(iPage, MaterialListResult::new);
        return Result.ok(zPage);
    }

    @ApiOperation("物料下拉框")
    @GetMapping("/list")
    Result<List<MaterialListResult>> list2() {
        List<Material> list = materialService.list(new LambdaQueryWrapper<Material>().select(Material::getId, Material::getName));
        return ok(copyList(list, MaterialListResult::new));
    }

    @ApiOperation("新增")
    @PostMapping
    @Authorization("material.add")
    @UserLog(desc = "内部倒运-新增物料管理", roleCode = "material.add")
    Result<String> add(@RequestBody @Validated MaterialAddParam param) {
        Material entity = CglibUtil.copy(param, Material.class);
        String record = String.format("物料名称：%s", entity.getName());
        return materialService.save(entity) ? Result.ok(record) : Result.err("保存失败");
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    @Authorization("material.del")
    @UserLog(desc = "内部倒运-删除物料管理", roleCode = "material.del")
    Result<String> del(@PathVariable Integer id) {
        int count = taskService.count(
                new LambdaQueryWrapper<Task>()
                        .eq(Task::getMaterialId, id));
        if (count > 0) return Result.err("请先删除物料对应的派车单");
        int count1 = plannedOrdersService.count(new LambdaQueryWrapper<PlannedOrdersEntity>().eq(PlannedOrdersEntity::getMaterialId, id));
        if (count1 > 0) return Result.err("请先删除物料对应的计划单");

        Material material = materialService.getById(id);
        String record = String.format("物料名称：%s", material == null ? "" : material.getName());
        return materialService.removeById(id) ? Result.ok(record) : Result.err("删除失败,此ID不存在或其他异常");
    }

    @ApiOperation("修改")
    @PostMapping(value = "/update")
    @Authorization("nbdy.material.update")
    @UserLog(desc = "内部倒运-修改物料管理", roleCode = "nbdy.material.update")
    public Result<Object> delete(@RequestBody MaterialAddParam params) throws ResultException {
        if (StrUtil.isNotBlank(params.getName())) {
            LambdaQueryWrapper<Material> query1 = new LambdaQueryWrapper<>();
            query1.eq(Material::getName, params.getName());
            query1.ne(Material::getId, params.getId());
            List<Material> list = materialService.list(query1);
            if (CollUtil.isNotEmpty(list)) {
                throw new ResultException(params.getName() + "已存在");
            }
        }
        Material entity = CglibUtil.copy(params, Material.class);
        materialService.updateById(entity);
        return Result.ok(HttpStatus.OK);
    }

}